package com.codeoss.framework.view.component.store;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.util.PageUtil;

@Component
public class ComponentStore implements IComponentStore {
    private Map<String,Page> pageCache=new HashMap<String,Page>();
    @Autowired
    private PageUtil pageUtil;
	@Override
	public void put(String code,Page page) {
		// TODO Auto-generated method stub
			pageCache.put(code, page);	
	}

	@Override
	public Page get(String code) {
		// TODO Auto-generated method stub
			Page page=pageCache.get(code);
			if(page==null) {
				page=init(code);
				pageCache.put(code, page);
			}
			return page;
	}

	@Override
	public Page remove(String code) {
		// TODO Auto-generated method stub
		return pageCache.remove(code);
	}
	
	private Page init(String code) {
		return pageUtil.getPageByPageCode(null,code);
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		pageCache.clear();
	}

	@Override
	public Map<String, Page> getCache() {
		// TODO Auto-generated method stub
		return pageCache;
	}
}
